package org.amse.ys.sndeditor.ui;

import java.io.File;

import org.amse.ys.sndeditor.model.Localiser;
import org.amse.ys.sndeditor.model.filesearcher.FileSearcher;
import org.amse.ys.sndeditor.ui.R.string;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class SoundOpenActivity extends ListActivity {
	FileListAdapter myAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Context c = this;
		final Notifyer n = new Notifyer();
		n.notToShow = false;
		n.dialog = null;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				synchronized (n) {
					if (!n.notToShow) {
						n.dialog = ProgressDialog
								.show(
										c,
										"",
										Localiser
												.getString(org.amse.ys.sndeditor.ui.R.string.searching_for_media),
										true);
					}
				}
				Looper.loop();
			}
		}).start();

		myAdapter = new FileListAdapter(this);
		setListAdapter(myAdapter);
		FileSearcher.findSupportedFiles();
		myAdapter.refreshFileList();
		myAdapter.notifyDataSetChanged();
		synchronized (n) {
			if (n.dialog != null) {
				n.dialog.cancel();
			}
			n.notToShow = true;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent();
		intent.setClass(this, Soundeditor.class);
		File f = myAdapter.getFile(position);
		intent.putExtra(FileOpener.FILE_NAME, f.getAbsolutePath());
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0,
				Localiser.getString(string.switch_to_file_browser)).setIcon(
				org.amse.ys.sndeditor.ui.R.drawable.browser);
		menu.add(0, Menu.FIRST + 1, 0, Localiser.getString(string.refresh))
				.setIcon(org.amse.ys.sndeditor.ui.R.drawable.refresh);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			Intent intent = new Intent();
			intent.setClass(this, FileOpener.class);
			startActivity(intent);
			finish();
			return true;
		case (Menu.FIRST + 1):
			final Context c = this;
			final Notifyer n = new Notifyer();
			n.notToShow = false;
			n.dialog = null;
			new Thread(new Runnable() {
				@Override
				public void run() {
					Looper.prepare();
					synchronized (n) {
						if (!n.notToShow) {
							n.dialog = ProgressDialog
									.show(
											c,
											"",
											Localiser
													.getString(org.amse.ys.sndeditor.ui.R.string.searching_for_media),
											true);
						}
					}
					Looper.loop();
				}
			}).start();
			FileSearcher.findAgainSupportedFiles();
			myAdapter.refreshFileList();
			myAdapter.notifyDataSetChanged();
			synchronized (n) {
				if (n.dialog != null) {
					n.dialog.cancel();
				}
				n.notToShow = true;
			}
			return true;
		}
		return false;
	}

}

class Notifyer {
	public boolean notToShow;
	ProgressDialog dialog;
}